CONTENTS | INDEX | PREV | NEXT
 onbreak

 NAME
  onbreak - set special ^C handler    (not ANSI)

 SYNOPSIS
  typedef int (*fptr)();

  fptr oldfunc = onbreak(newfunc);
  fptr newfunc;

 FUNCTION
  onbreak() sets a special function to handle ^C.  It takes a pointer to
  this function and returns a pointer to the previous onbreak function,
  if any.  When ^C is hit, the special onbreak function is called before
  any other action.

  If the onbreak function returns a non-zero value, ^C aborts the program
  like it usually does.  If the function returns 0, however, the ^C
  is completely ignored.

 EXAMPLE
  /*
   *  note: The reentrancy check is needed because of both the puts
   *    and the sleep() all.
   */

  #include <stdio.h>
  #include <stdlib.h>

  int
  brk()
  {
      static short cnt = 0;   /*  check for reentrancy */

      if (cnt)                /*  if not 0 then reentered!    */
      return(0);
      ++cnt;

      puts("Nah Nah, you can't break me!");
      sleep(1);
      --cnt;
      return(0);
  }

  int
  main()
  {
      short i;

      onbreak(brk);
      puts("Hit ^C while I loop from 1 to 100, as many times as you want");
      sleep(2);
      for (i = 1; i <= 100; ++i)
      printf("Loop, counting, count = %dn", i);
      return(0);
  }

 SEE ALSO
  atexit